All right.
The next trap that we can work on are going to be the landmines that will explode when a player touches
them.
The models for the landmines are very simple.
We can go ahead and take a look at one of them.
For example, in RB model number eight.
And if we go find that RB model, as you can see here's one of our landmines.
It's just two parts with an invisible part on top of it.
And we're going to check when a player touches this invisible part.
And when they do we're going to make the landmine explode.
So I'll parent this back into replicated storage.
And we're just going to follow the same process that we did for the other traps.
We'll create a new module script inside of starter player scripts in the handler section.
I'm going to call this script my landmine handler.
And we'll paste in that exact same template that we've used before, and I'll rename this service to
be my land mine handler.
We're only going to need an initialize function as well as the collection service.
We can go ahead and define the tag for the service.
So that's going to be the tag of landmine.
And then inside of our initialize function, we'll refer to the collection service get instance added
signal, pass our tag here and connect a lambda function to this to get that instance.
And then we can go ahead and create another module script that's going to be our class.
We'll call this our Land Mine class.
And we'll be using the same class template as before.
We'll paste that in there.
I'll rename this to our Land Mine class and I'll rename this to Land Mine Objects.
And now we can go ahead and require our landmine class.
So landmine class is equal to require script dot landmine class.
And then we can go ahead and use the constructor.
All right inside of the class, we can go ahead and grab the player service to get a reference to our
local player.
And then we can also get the sound service to grab the explosion sound for our land mind.
And then we can go ahead and make a reference to those two things.
So local player is going to be equal two players local player.
And then we can get our explosion sound that's equal to sound service dot SFX dot explosion.
And then we can also define a constant for how long we should wait before responding the landmine so
we can explode the landmine.
We'll wait a set period of time and then make the landmine explode again.
So we can just call this respawn duration.
And set it to something like five seconds.
And then inside of our constructor, we can go ahead and get a reference to the model.
And then we can also store a boolean.
We'll call on Cool Down to be our Debounce.
And then we'll listen to when that detect zone part inside of our landmine gets touched.
And we'll connect a function, get that part that touched it, and then we'll check if that's the player.
So we'll use player's get player from character pass other part dot parent.
And if we do not have a player, then we're just going to return.
Otherwise we can go ahead and explode the landmines.
So we'll create a dedicated function for that.
We'll just call it explode.
And then we can have another function to restore the landmine back to its original state.
And then we'll call self dot explode right here.
And inside of this function, we want to make sure that we're not on our cooldown.
If we are, then we're going to return.
Otherwise, we'll set the cooldown equal to true.
And then what we could do is we could create a new explosion instance.
So I'll call this explosion that's equal to instance dot new explosion.
And then we'll also clone our explosion sound.
And then we'll set play on remove equal to true as well, and then set the parent of this sound equal
to.
We can set it equal to the primary part of our landmine.
So our landmine has a primary part set to it which is one of the parts inside of the landmine.
So we'll do self dot model and get the model's primary part.
And then we can go ahead and set some of the properties for our explosion instance.
So for the explosions blast radius we could do something like 20 studs.
We'll set the explosions blast pressure equal to something like we could just do like 10,000.
It doesn't really matter.
The explosion dot destroy joint radius percent.
We're going to set that to zero to not destroy the player's joints.
And then the position of this explosion in the workspace is going to be equal to our self dot model
dot primary part.
And we're going to get that position of the primary part for our landmine.
And before we apparent this explosion to the workspace, we want to listen to the explosions, hit event,
connect a function, and get that hit part.
And we want to make sure that this hip part belongs to our local player.
So we'll create a variable.
We'll call it hip player.
And that's going to be equal to players.
Get player from character and pass hit part dot parent.
And if this hit player variable is not equal to our local player.
So if it's a nil or if it's somehow another player then we're just going to return.
Otherwise we can go ahead and refer to our local player, get our character and our humanoid, and we
can go ahead and change the state on our humanoid so we can do change state to the enum dot humanoid
state type dot dead.
So this will kill our player's character.
After we've done that, then we can go ahead and parent the explosion equal to, um, the workspace.
Or we could set it equal to the self dot model.
So that means the explosion will happen.
And at the same time we want to go ahead and destroy the sound instance.
So it plays the explosion sound.
And once we do that we also want to I guess make our landmine disappear.
So we could do self dot model.
The first part of our landmine is called top.
And we'll just set the transparency of that equal to one.
And then the other part of our landmine is called the body.
And we'll set that transparency equal to one as well.
And then we can go ahead and delay a function for that respawn duration.
And that's where we would call our restore function.
So we'll pass self dot restore and then pass self.
And then inside of this function, we'll check if our landmine wasn't destroyed.
So if we don't have a reference to the landmine in our self table, meaning we have cleaned up, um,
this particular object, or if the model's parent is equal to nil, then we're just going to return.
Otherwise we can just copy this right here.
Set the transparency to be opaque.
And then set self dot on cool down equal to false.
And then we could also go ahead and clean up the previous explosion instance that's parented inside
of our model, if it exists.
So we'll create a variable, I'll call it explosion.
And that's going to be equal to self dot model find first child which is a.
Explosion.
So if we found an explosion, then we can just go ahead and destroy it.
And then I'll just move this above where we set cooldown equal to false.
So now any time a landmine gets touched in our game, we should be able to blow it up, play an explosion
sound.
And if our player is within range of that explosion, then our player should die.
So I think that's all we need to do.
Let's go ahead and test out to see if this is working.
We'll play the game.
And we get no errors in the console, which is always good, but that doesn't mean any landmines spawn,
so let's keep a keen eye out for any landmines.
Okay.
Here I found one of the landmines.
And if I go and touch it, hopefully it should explode and kill me.
So if I touch it, there we go.
Explosion sound.
My character dies and we move back to where we were at, and it looks like it has successfully respawn.
So if I touch it again, there we go.
It blows up, I die.
And then we come back.
Let me see if we can see the parts actually disappear.
So if I touch it one more time.
Yep, those parts disappeared.
And then five seconds later, the landmine responds itself and it continues to blow us up.
Very cool.
We have one more trap to script and that's going to be done in the next lecture.
See you there.